473,420 Members | 1,618 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,420 software developers and data experts.

URLdecode ?

Hi,

Is there an equivalent to URLdecode() in javascript ?

Details:
In Asp.Net I am using
HttpContext.Current.Response.Redirect("genericerro r.htm?ErrStr=" &
Err.Number & ": " & Err.Description)

In genericerror.htm I have:

<SCRIPT LANGUAGE="javascript">
var url = window.location.href;
var urlPos = url.indexOf('?') + 1;
var urlLength = url.length - urlPos;

if (urlPos > 0) {
url.substr(url, urlPos, urlLength);
url.split('&');
var values = url.split('&');
value = values[0].split('=');
document.write(value[1]);
}
</SCRIPT>

But I have problems with whitespace (and perhaps later other "funny" chars)
so how do I make the string "safe" or "back to normal" ?

tia
/jim
Jul 20 '05 #1
13 17558
I beleive you want encodeURI and decodeURI (or maybe encodeURIComponent and
decodeURIComponent)

"Jim Andersen" <ji*@officeconsult.dk> wrote in message
news:bh**********@sunsite.dk...
Hi,

Is there an equivalent to URLdecode() in javascript ?

Details:
In Asp.Net I am using
HttpContext.Current.Response.Redirect("genericerro r.htm?ErrStr=" &
Err.Number & ": " & Err.Description)

In genericerror.htm I have:

<SCRIPT LANGUAGE="javascript">
var url = window.location.href;
var urlPos = url.indexOf('?') + 1;
var urlLength = url.length - urlPos;

if (urlPos > 0) {
url.substr(url, urlPos, urlLength);
url.split('&');
var values = url.split('&');
value = values[0].split('=');
document.write(value[1]);
}
</SCRIPT>

But I have problems with whitespace (and perhaps later other "funny" chars) so how do I make the string "safe" or "back to normal" ?

tia
/jim

Jul 20 '05 #2
kaeli wrote:
In article <bh**********@sunsite.dk>, ji*@officeconsult.dk enlightened
us with...
But I have problems with whitespace (and perhaps later other "funny"
chars) so how do I make the string "safe" or "back to normal" ?


var x = unescape(url);


thx !

Along the same lines...

In .NET I use RegisterClientScriptBlock as a kind of msgbox.
msgStr="Hello"
pageref.RegisterClientScriptBlock("aaa", "<script language=JavaScript>
alert('" & msgstr & "')</SCRIPT>")

works great. BUT, not if msgStr="Hel'lo" (with a single quote). And the
content of msgStr is of course unknown to me at runtime.

Whats the syntax for escaping msgstr ?

tia
/jim

Jul 20 '05 #3
In article <bi**********@sunsite.dk>, ji*@officeconsult.dk enlightened
us with...
In .NET I use RegisterClientScriptBlock as a kind of msgbox.
msgStr="Hello"
pageref.RegisterClientScriptBlock("aaa", "<script language=JavaScript>
alert('" & msgstr & "')</SCRIPT>")

works great. BUT, not if msgStr="Hel'lo" (with a single quote). And the
content of msgStr is of course unknown to me at runtime.

Whats the syntax for escaping msgstr ?


Technically? A backslash before any quotes. That's the hard way, though.
Make the single quotes into double quotes and escape them so they don't
mess up your VB string. Then you won't have to worry about single
quotes, but double quotes will still mess you up.
In VB, IIRC, to escape a double quote literal you preface it with
another double quote. In javascript, you use a backslash.

alert(""" & msgstr & """)</SCRIPT>

You could always parse msgstr and put a single backslash before any
quotes, but I think that's overcomplicating things if all you need to
worry about are single quotes.
-------------------------------------------------
~kaeli~
Press any key to continue or any other key to quit.
Who is General Failure and why is he reading
my hard disk?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
Jul 20 '05 #4
kaeli wrote:
Whats the syntax for escaping msgstr ?
In javascript, you use a backslash.

alert(""" & msgstr & """)</SCRIPT>

You could always parse msgstr and put a single backslash before any
quotes, but I think that's overcomplicating things if all you need to
worry about are single quotes.


yes... but is that all I need to worry about ? What else could freak that
javascript out of working ?

/jim
Jul 20 '05 #5
In article <bi**********@sunsite.dk>, ji*@officeconsult.dk enlightened
us with...

alert(""" & msgstr & """)</SCRIPT>

You could always parse msgstr and put a single backslash before any
quotes, but I think that's overcomplicating things if all you need to
worry about are single quotes.


yes... but is that all I need to worry about ? What else could freak that
javascript out of working ?


Just having embedded double quotes, AFAIK.

I don't think any other normal characters would be a problem. I assume
since you have control of the string, no really odd characters (like
non-printing chars or foreign chars) would be entered.

-------------------------------------------------
~kaeli~
Press any key to continue or any other key to quit.
Who is General Failure and why is he reading
my hard disk?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
Jul 20 '05 #6
kaeli wrote:
In article <bi**********@sunsite.dk>, ji*@officeconsult.dk enlightened
us with...

alert(""" & msgstr & """)</SCRIPT>

You could always parse msgstr and put a single backslash before any
quotes, but I think that's overcomplicating things if all you need
to worry about are single quotes.


yes... but is that all I need to worry about ? What else could freak
that javascript out of working ?


Just having embedded double quotes, AFAIK.

I don't think any other normal characters would be a problem. I assume
since you have control of the string, no really odd characters (like
non-printing chars or foreign chars) would be entered.


I actually do
msgstr=err.Number & ": " & err.Description
and once .Description was
no such object 'zzzTblTst'
I am gonna experiment with server.getlasterror etc, and that could (AFAIK)
include both [ and & and { etc.

/jim
Jul 20 '05 #7
>> But I have problems with whitespace (and perhaps later other "funny"
chars)
so how do I make the string "safe" or "back to normal" ?
var x = unescape(url);


hmmm, nope.

Have searched hi&lo on google and it seems it's a known issue. .Net and Java
doesn't use the same algorithm for encoding-decoding. So I don't get my
original string.

/jim
Jul 20 '05 #8
On Thu, 4 Sep 2003 16:54:20 +0200, "Jim Andersen"
<ji*@officeconsult.dk> wrote:
Have searched hi&lo on google and it seems it's a known issue. .Net and Java
doesn't use the same algorithm for encoding-decoding. So I don't get my
original string.


decodeURIComponent

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #9
Jim Ley wrote:
On Thu, 4 Sep 2003 16:54:20 +0200, "Jim Andersen"
<ji*@officeconsult.dk> wrote:
Have searched hi&lo on google and it seems it's a known issue. .Net
and Java doesn't use the same algorithm for encoding-decoding. So I
don't get my original string.


decodeURIComponent

Jim.


So why isn't this working:

foo.aspx:
Response.Redirect("tst.htm?msg=" & HttpUtility.UrlEncode("6: division by
zero"))

tst.htm
<body>
Msg=
<SCRIPT LANGUAGE="javascript">
var url = window.location.href;
var urlPos = url.indexOf('?') + 1;
var urlLength = url.length - urlPos;
if (urlPos > 0) {
url.substr(url, urlPos, urlLength);
url.split('&');
var values = url.split('&');
value = values[0].split('=');
document.write( value[1]);
}
</SCRIPT>
</body>

Result: I get Msg= 6%3a+division+by+zero

I change tst.htm:
var=FunctionToGetQuerystring;
document.write(decodeURIComponent(var))

Result: I get Msg= 6:+division+by+zero

If I change foo.aspx:
Response.Redirect("tst.aspx?msg=" & HttpUtility.UrlEncode("6: division by
zero"))

and have tst.aspx do:
Response.Write("msg=" & HttpUtility.UrlDecode(Request.QueryString("msg")))
I finally get the desired result
msg=6: division by zero

regards
Jim Andersen
Jul 20 '05 #10
On Fri, 5 Sep 2003 12:10:18 +0200, "Jim Andersen"
<ji*@officeconsult.dk> wrote:
foo.aspx:
Response.Redirect("tst.htm?msg=" & HttpUtility.UrlEncode("6: division by
zero"))
Result: I get Msg= 6:+division+by+zero


It looks like your HttpUtility.UrlEncode is not performing correct URI
encoding, it appears to be encoding space as +, this is not correct,
your problem is with that, not with javascript.

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #11
ji*@jibbering.com (Jim Ley) writes:
It looks like your HttpUtility.UrlEncode is not performing correct URI
encoding, it appears to be encoding space as +, this is not correct,


Well, for some definition of correct, it is. It is the same way form
data are encoded for the GET transfer method.

The solution is to change all plusses to spaces before decoding with
unescape:

var decoded = unescape(encoded.replace(/\+/g," "));

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #12
On 05 Sep 2003 12:48:38 +0200, Lasse Reichstein Nielsen
<lr*@hotpop.com> wrote:
ji*@jibbering.com (Jim Ley) writes:
It looks like your HttpUtility.UrlEncode is not performing correct URI
encoding, it appears to be encoding space as +, this is not correct,


Well, for some definition of correct, it is. It is the same way form
data are encoded for the GET transfer method.


True, but it is not URIEncoding which the method name suggests.

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #13
Lasse Reichstein Nielsen wrote:
The solution is to change all plusses to spaces before decoding with
unescape:

var decoded = unescape(encoded.replace(/\+/g," "));


Thx Lasse for the helpful answer (as opposed to Jim Ley's fantasies).

/jim
Jul 20 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Henz | last post by:
Hi again, can anyone tell me why it says that the object doesnt support this method? rs is a db query resultset. msg is a db field with urlencoded data in it. ------------ if not rs.eof then...
0
by: Yifan | last post by:
Hi I want to use UrlDecode() in C++. I have a Win32 Visual C++ (.NET) project #using <mscorlib.dll #using <System.dll #using <System.Web.dll using namespace System using namespace...
1
by: Joăo Santa Bárbara | last post by:
hi all is there any replace for the System.Web.HttpUtility.UrlDecode to windows forms ... thks JSB
5
by: Yifan | last post by:
Hi I want to use UrlDecode() in C++. I have a Visual C++ (.NET) Win32 project #using <mscorlib.dll #using <System.dll #using <System.Web.dll using namespace System using namespace...
4
by: John Hoge | last post by:
Hi, I need to UrlDecode a string in a simple console application, but I am having trouble accessing Server.UrlDecode, even though System.Web is included. The application pulls a text out of a...
6
by: John Grandy | last post by:
Does Server.URLDecode() need to be applied to Request.QueryString(<key name>) ?
10
by: Alex | last post by:
Hi, I UrlEncode a value that I put in my querystring. This value originally contains the character "č", which is UrlEncoded to "%E8". When I try to read the value from the querystring, I find...
1
by: fjm67 | last post by:
I see Google edited my email address. Lets try this. captonline at yahoo dot com Can you guys help me out please? I need to implement urldecode into a php script and don't know how. Here is...
20
by: gert | last post by:
This based on a example i found at http://www.cs.tut.fi/~jkorpela/ forms/cgic.html #include <fcgi_stdio.h> #include <stdlib.h> int urldecode(char *src, char *last, char *dest){ int code;...
3
by: gert | last post by:
Anybody can tell me what i need to import to make urlDecode() work in python2.5 please. import urllib urllib.urlDecode(post) #doesn't exist urllib.urldecode(post) #doesn't exist...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.